package com.kdtech.analyse.NewsPaper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * http://szb.northnews.cn/nmgrb/html/2012-12/04/node_61.htm 内蒙古日报
 * http://szb.northnews.cn/bfxb/html/2012-12/04/node_2.htm 北方新报
 * http://szb.northnews.cn/bfzm/html/2012-11/29/node_132.htm 北方周末报
 * http://szb.northnews.cn/xzm/html/2012-11/25/node_402.htm 北方新报#新周末
 * @author KK
 *
 */
public class NorthnewsNewsPaperAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://szb.northnews.cn/nepaper/(bfxb|nmgrb)/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*.htm",
				};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();


		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		Document doc = Jsoup.parse(html);

		title =doc.select("h1").text();
		dateStr = url;
		date=DateUtils.matchDate(dateStr);

		content =HtmlCleaner.getContentHtml(url,doc.select("div#ozoom"));

			NewsMeta newspaper = new NewsMeta();
			newspaper.setUrl(url);
			newspaper.setTitle(StringUtils.trimSpace(title));
			newspaper.setContent(StringUtils.trimSpace(content));
			newspaper.setDate(date);
			return newspaper;


	}

	
	public boolean isTaskPage(String url) {
		String[] regex = {
				"http://szb.northnews.cn/nepaper/(bfxb|nmgrb)/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/node_[0-9]*.htm",
				};
		return RegexUtils.matchAny(url, regex);
	}

	
}
